using System.Collections.Generic;
using UnityEngine;

public class ZombieDeployer : MonoBehaviour
{
	public List<Zombie> zombies;

	public float interval;

	private bool deployed;

	private int deployedCount;

	private bool deployable;

	private float nextDeployTime;

	private Player player;

	private void Start()
	{
		player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
	}

	private void FixedUpdate()
	{
		DeployOne();
	}

	private void DeployOne()
	{
		if (deployable && !deployed && !(nextDeployTime > Time.time))
		{
			GameObject gameObject = Ground.groundColliders[Random.Range(0, Ground.groundColliders.Count)];
			float x = player.transform.position.x + 12f;
			float y = gameObject.transform.position.y;
			Zombie component = Object.Instantiate(zombies[deployedCount], new Vector2(x, y), Quaternion.identity).GetComponent<Zombie>();
			component.player = player;
			component.body.SetColliderLayer(gameObject.layer);
			deployedCount++;
			nextDeployTime = interval + Time.time;
			if (deployedCount >= zombies.Count)
			{
				deployed = true;
			}
		}
	}

	private void OnTriggerEnter2D(Collider2D collision)
	{
		if (collision.gameObject.tag == "Player")
		{
			deployable = true;
		}
	}
}
